package medium

type ListNode445 struct {
	Val  int
	Next *ListNode445
}

func addTwoNumbers(l1 *ListNode445, l2 *ListNode445) *ListNode445 {
	var stack1, stack2 []int

	for l1 != nil {
		stack1 = append(stack1, l1.Val)
		l1 = l1.Next
	}

	for l2 != nil {
		stack2 = append(stack2, l2.Val)
		l2 = l2.Next
	}

	node := &ListNode445{}
	for len(stack1) > 0 || len(stack2) > 0 {
		sum := node.Val

		if len(stack1) > 0 {
			sum += stack1[len(stack1)-1]
			stack1 = stack1[:len(stack1)-1]
		}

		if len(stack2) > 0 {
			sum += stack2[len(stack2)-1]
			stack2 = stack2[:len(stack2)-1]
		}
		node.Val = sum % 10
		node = &ListNode445{
			Val:  sum / 10,
			Next: node,
		}
	}
	if node.Val == 0 {
		return node.Next
	}

	return node
}
